var urlip = '116.85.19.102';
	localStorage.setItem('urlip',urlip)
	
var sendTaskTimer = null;

var urlip = localStorage.getItem('urlip');
var url = 'http://'+ urlip +':8080/platform-web/';
var userid = localStorage.getItem('userid');

//after five minutes send task start
function setTimeSendTaskStart(){
	sendTaskTimer = setInterval(function(){
		sendTask(url,userid)
	},5*60*1000);
}

//after five minutes send task end
function setTimeSendTaskEnd(){
	clearInterval(sendTaskTimer);
}

//send task
function sendTask(url,userid){
	var privateState = localStorage.getItem('privatestate');
	var protocolState = localStorage.getItem('protocolstate');
	if(userid){
		// console.log((typeof privateState),protocolState)
		if(privateState === 'true' && protocolState === 'true'){
			$.ajax({
				type:'POST',
				url:url+'user/getTask',
				dataType:'json',
				data:{
					id:userid
				},
				success:function(data){
					if(data.code === 403){
						location.href = './login.html';
					}else if(data.code === 404){
						alert('设备被锁');
						location.href = './login.html';
					}else if(data.code === 5000 ){
						var currentTime = new Date(data.nowDate).getTime();
						var getTime = new Date(data.getTime).getTime();
						var calcTime = getTime-currentTime;
						//show next task
						showNextTask(calcTime,data);
						
						/* $('#stateLayer').show();
						$('#stateicon').removeAttr('class').addClass('tipsicon');
						$('#stateContext').text('接到任务');
						
						setTimeout(function(){
							$('#stateLayer').hide();
						},3000) */
						
					}else if(data.code === 5001 ){
						
						/* $('#stateLayer').show();
						$('#stateicon').removeAttr('class').addClass('tipsicon');
						$('#stateContext').text('暂未接到任务');
						
						setTimeout(function(){
							$('#stateLayer').hide();
						},5000) */
					}else if(data.code === 5002 ){
						//after five minutes send task end
						setTimeSendTaskEnd();
					}
				},
			})
		}else{
			
			localStorage.setItem('taskstate',false)
			//after five minutes send task end
			setTimeSendTaskEnd();
			$('#enginBtn').children('img').attr('src','./img/icons/start.png');
			
			//set private and protocol
			localStorage.setItem('ppkey',true);
			location.href = "./setup.html?tips=1";
			
		}
	}else{
		setTimeout(function(){
			location.href = './login.html'
		},1500)
	}
}

function closeModal(){
	setTimeout(function(){
		$('#inforModal').hide();
	},2000); 
}


// downLoadAlertHtml(4,0.01)
// $('#divmodalTitleColor').addClass('divmodal-title-color');
// $('#divmodalBodybg').addClass('divmodal-body-bg');

// 模拟数据
/* 
var data = {
	apkImages: '["https://pp.myapp.com/ma_pic2/0/shot_1102218_1_1552527914/550","https://pp.myapp.com/ma_pic2/0/shot_1102218_2_1552527914/550","https://pp.myapp.com/ma_pic2/0/shot_1102218_3_1552527914/550","https://pp.myapp.com/ma_pic2/0/shot_1102218_4_1552527914/550","https://pp.myapp.com/ma_pic2/0/shot_1102218_5_1552527914/550"]',
	apkUrl: "https://imtt.dd.qq.com/16891/739445E8F73F3B7A4AB4D9E6D6684911.apk?fsname=com.focustech.xyz_4.24.10_4241099.apk&csr=1bbd",
	code: 5000,
	commission: "0.10",
	getTime: "2019-03-20 22:05:58",
	nowDate: "2019-03-20 22:05:57",
	taskType: 2
}
 
showNextTask(1000,data) */

//show next task
function showNextTask(calcTime,data){
	
	//'1注册2下载3广告';
	setTimeout(function(){
		setStyle(data);
	},calcTime);
	
}

//set task style
function setStyle(data){
	
	var taskType = data.taskType;
	
	if(taskType === 1){
		//register task
		registerTask(data);
		//create advertisement 
		createAdertisementHtml();
	}else if(taskType === 2){
		//download task
		downloadTask(data);
		//create advertisement 
		createAdertisementHtml();
	}else if(taskType === 3){
		//advertisement task
		advertisementTask(data);
		//create advertisement 
		createAdertisementHtml();
	}

}

//register task 1
function registerTask(data){
	
	var delaytime = 0;
	var showtimer = setInterval(function(){
		delaytime++;
		if(delaytime === 1){
			//task style write
			downLoadAlertHtml(5);
			$('#divmodalHead').addClass('divmodal-headbg');
			$('#taskdiv').fadeIn(600);
			closeTaskdivmodal();
		}else if(delaytime === 4){
			//show app img
			showAppImg(data);
			var getMoney = data.commission;
			//task style write
			downLoadAlertHtml(4,getMoney);
			
			$('#divmodal').css('background','none');
			$('#divmodalTitleColor').addClass('divmodal-title-color');
			$('#divmodalBodybg').addClass('divmodal-body-bg');
			
		}else if(delaytime === 5){
			delaytime = 0;
			clearInterval(showtimer);
		}
	},1000);

}


//download task  2
function downloadTask(data){
	
	var delaytime = 0;
	var showtimer = setInterval(function(){
		delaytime++;
		if(delaytime === 1){
			//task style write
			downLoadAlertHtml(1);
			$('#divmodalHead').addClass('divmodal-headbg');
			$('#taskdiv').fadeIn(600);
			closeTaskdivmodal();
		}else if(delaytime === 4){
			//task style write
			downLoadAlertHtml(2);
			$('#taskdiv').fadeIn(600);
			closeTaskdivmodal();
		}else if(delaytime === 8){
			//task style write
			downLoadAlertHtml(3);
			$('#taskdiv').fadeIn(600);
			closeTaskdivmodal();
		}else if(delaytime === 10){
			//show app img
			showAppImg(data);
			var getMoney = data.commission;
			//task style write
			downLoadAlertHtml(4,getMoney);
			
			$('#divmodal').css('background','none');
			$('#divmodalTitleColor').addClass('divmodal-title-color');
			$('#divmodalBodybg').addClass('divmodal-body-bg');
			
		}else if(delaytime === 11){
			delaytime = 0;
			clearInterval(showtimer);
		}
	},1000);
	
	
}

//advertisement task  3
function advertisementTask(data){
	
	var delaytime = 0;
	var showtimer = setInterval(function(){
		delaytime++;
		if(delaytime === 1){
			//task style write
			downLoadAlertHtml(6);
			$('#divmodalHead').addClass('divmodal-headbg');
			$('#taskdiv').fadeIn(600);
			closeTaskdivmodal();
		}else if(delaytime === 4){
			showAppImg(data);
		}else if(delaytime === 5){
			delaytime = 0;
			clearInterval(showtimer);
		}
	},1000);
	
}

//close task divmodal
function closeTaskdivmodal(){
	setTimeout(function(){
		$('#taskdiv').fadeOut(500);
	},1500);
}

//task style write
function downLoadAlertHtml(style,getMoney){
	var downloadHtml = '';
	var getMoney = getMoney||'0.00';
	if(style === 1){
		downloadHtml = createNoticeHtml('<span>恭喜你</span>','抢到任务！','准备加载应用软件...');
	}else if(style === 2){
		downloadHtml = createNoticeHtml('<span>试玩助手</span>','安装等待中！','服务器下载成功，安装中...');
	}else if(style === 3){
		downloadHtml = createNoticeHtml('<span>试玩助手</span>','应用安装完毕！','服务器安装完毕，试玩等待中，官方已验证');
	}else if(style === 4){
		downloadHtml = createNoticeHtml('<div class="divmodal-headimg"><span>恭喜您获得</span><img src="./img/award-head.png" /></div>',getMoney + '元','等待抢单任务...');
	}else if(style === 5){
		downloadHtml = createNoticeHtml('<span>试玩助手</span>','抢到任务','服务器注册中...');
	}else if(style === 6){
		downloadHtml = createNoticeHtml('<span>试玩助手</span>','任务分配','正在分配任务，请耐心等待...');
	}
	
	$('#taskdiv').html(downloadHtml);
}

// create task notice
function createNoticeHtml(headtext,title,context){
	return '<div class="divmodal" id="divmodal"><div class="divmodal-inner"><div class="divmodal-contain"><div class="divmodal-head" id="divmodalHead">'+
		headtext + '</div><div class="divmodal-body" id="divmodalBodybg"><div class="divmodal-content-title">'+
		'<span id="divmodalTitleColor">'+ title +'</span>'+
		'</div><div class="divmodal-content-text">'+
		'<span>'+ context +'</span>'+
		'</div></div></div></div></div>';
}

//create advertisement 
function createAdertisementHtml(){
	var apkHrefHtml = '<div class="apkHref" id="apkHrefModal"><div class="slide-button">'+
	'<div class="slide-button-contain"><div class="slide-button-img">'+
	'<img src="./img/icons/down.png" alt=""></div></div></div>'+
	'<div class="apkHref-title"><span>正在全屏模式下查看</span></div>'+
	'<div class="apkHref-content"><span>要退出，请从顶部向下滑动</span></div>'+
	'<div class="apkHref-btn"><button type="button" id="closeApkHrefModal">我知道了</button></div></div>'
	
	$('body').append(apkHrefHtml);
}

//show pic
function showAppImg(data){
	
	var apkImage = 'https://pp.myapp.com/ma_pic2/0/shot_12170842_1_1552577294/550';
	
	var apkImagesArr = '';
	var imghtml = '';
	if( data.apkImages && data.apkImages != '' ){
		apkImagesArr = JSON.parse(data.apkImages);
		imghtml = '<div class="divimg" id="preimg"><img src="'+ apkImagesArr[0] +'" alt=""></div>';
	}else{
		imghtml = '<div class="divimg" id="preimg"><img src="'+ apkImage +'" alt=""></div>' 
	}
	
   // var imghtml = '<div class="divimg" id="preimg"><img src="'+ apkImages +'" alt=""></div>' 
	
	$('body').append(imghtml);
	
	$('#preimg').animate({right:"0"},500);
	
	/* $('#apkHrefModal').click(function(){
		location.href = data.apkUrl;
	});
	
	$('#closeApkHrefModal').click(function(){
		$('#apkHrefModal').slideUp(600);
	}); */
	
	var imgtime = 0;
	var changeimgtimer = setInterval(function(){
		imgtime++;
		
		if(imgtime === 1){
			$('#apkHrefModal').slideDown(500);
			
			var getMoney = data.commission;
			//task style write
			downLoadAlertHtml(4,getMoney);
			$('#divmodal').css('background','none');
			$('#divmodalTitleColor').addClass('divmodal-title-color');
			$('#divmodalBodybg').addClass('divmodal-body-bg');
		}else if(imgtime === 2){
			$('#apkHrefModal').slideUp(500);
		}else if(imgtime === 3){
			$('#preimg').animate({right:'-100%'},800);
		}else if(imgtime === 4){
			$('#taskdiv').fadeIn(600);
		}else if(imgtime === 5){
			closeTaskdivmodal();
		}else if(imgtime === 6){
			//insert map
			insertMap(data);
		}else if(imgtime === 7){
			imgtime = 0;
			clearInterval(changeimgtimer);
		}
	},1000);
	
}

//insert map
function insertMap(data){
	var poshtml = '<div class="divpos" id="mapContain"><div id="container" class="divmap"></div></div>'; 
	$('body').append(poshtml);
	
	var posNoticeHtml = function(ctx){
			return '<div class="divsmallmodal" id="divsmall"><div class="divsmallmodal-contain"><div class="divsmallmodal-content">'+
						'<span id="posContext">'+ ctx +'</span>'+
						'</div></div></div>';
		}
	
	$('body').append(posNoticeHtml('正在重新虚拟定位'));
	showPosition(posNoticeHtml);
	$('#divsmall').fadeIn(500);
	
	setTimeout(function(){
		$('#mapContain').animate({right:"0"},600);
		
		//set commission
		if( location.href.indexOf('index.html') >= 0 ){
			var getCommission = data.commission != '' ? parseFloat(data.commission).toFixed(2) : 0.00;
			var totalMoney = parseFloat( $('#totalMoney').text() ).toFixed(2);//total money
			var taskMoney = parseFloat( $('#taskMoney').text() ).toFixed(2);//commission money
			var usableMoney = parseFloat( $('#usableMoney').text() ).toFixed(2);//usable money 
		
			totalMoney = ( parseFloat(getCommission) + parseFloat(totalMoney) ).toFixed(2);
			taskMoney = ( parseFloat(getCommission) + parseFloat(taskMoney) ).toFixed(2);
			usableMoney = ( parseFloat(getCommission) + parseFloat(usableMoney) ).toFixed(2);
			
			$('#totalMoney').text(totalMoney);
			$('#taskMoney').text(taskMoney);
			$('#usableMoney').text(usableMoney);
		}
	},1000)
}

//show positon
function showPosition(posNoticeHtml){
	
	setTimeout(function(){
		$('#mapContain').animate({right:"-100%"},600);
		$('#posContext').text('重新虚拟定位成功');
		$('#preimg').remove();
		closePosTips();
	},4000);
	
	//初始化
	mui.init();
	
	//show map
	showMap();
}

//close pos tips
function closePosTips(){
	setTimeout(function(){
		$('#divsmall').fadeOut(500);
		$('#divsmall').remove();
		$('#mapContain').remove();
	},1000);
}

//show map
function showMap(){
	
	//第一步通过mui.plusReady
	mui.plusReady(function(){
		plus.geolocation.getCurrentPosition( geoInf, function ( e ) {},{geocode:true,provider:'amap'});
	})
	
	//第二步通过geolnf 方法来获取具体的定位信息
	function geoInf( position ) {
		var codns = position.coords;//获取地理坐标信息；
		var longt = codns.longitude;//获取到当前位置的经度
		var lat = codns.latitude;//获取到当前位置的纬度；
		
		localStorage.setItem('longt',longt)
		localStorage.setItem('lat',lat)
	}
	
	//初始化地图
	var jdXmin = 84.022152;
	var jdXmax = 121.626226;
	var wdYmax = 41.356937;
	var wdYmin = 30.624202;
	var longt = Math.random()*(jdXmax-jdXmin+1)+jdXmin;
	var lat = Math.random()*(wdYmax-wdYmin+1)+wdYmin;
	setTimeout(function(){
		addMarker(longt,lat);
	},3000)
	
	var marker,map = new AMap.Map('container', {
	  resizeEnable: false, //是否监控地图容器尺寸变化
	  zoom: 5, //初始地图级别
	  center: [longt, lat], //初始地图中心点
	});
	
	
	// 实例化点标记
	function addMarker(x,y) {
		marker = new AMap.Marker({
			icon: "./img/icons/poi-marker-default.png",
			position: [x,y],
			offset: new AMap.Pixel(-13, -30)
		});
		marker.setMap(map);
	}
}
	
	
